<template>
  <view class="category">
    <SearchLink></SearchLink>
    <view class="content">
      <view class="left">
        <view
          class="cont-item"
          :class="{ active: activeIndex === index }"
          @click="itemClick(index)"
          v-for="(item, index) in categroyData"
          :key="item.cat_id"
        >
          <view class="cont-text">{{ item.cat_name }}</view>
        </view>
      </view>
      <scroll-view
        class="right"
        :scroll-top="scrollTop"
        scroll-y="true"
        @scroll="scroll"
      >
        <view>
          <image
            class="title-img"
            src="../../static/images/titleImage.png"
          ></image>
          <view
            class="inner"
            v-for="item in categroyData[activeIndex].children"
            :key="item.cat_id"
          >
            <view class="title"
              ><text>/</text>{{ item.cat_name }}<text>/</text></view
            >
            <view
              class="inner-item"
              v-for="v in item.children"
              :key="v.cat_id"
              @click="search(v.cat_name)"
            >
              <image :src="v.cat_icon"></image>
              <text>{{ v.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import SearchLink from '../../components/SearchLink.vue'
export default {
  data () {
    return {
      activeIndex: 0,
      categroyData: [],
      old: {
        scollTop: 0
      },
      scrollTop: 0
    }
  },
  methods: {
    scroll (e) {
      this.old.scrollTop = e.detail.scrollTop
    },
    itemClick (index) {
      this.scrollTop = this.old.scrollTop
      this.$nextTick(() => {
        this.scrollTop = 0
      })
      this.activeIndex = index
    },
    search (name) {
      console.log(name)
      uni.navigateTo({
        url: '../search/search?cat_name=' + name
      })
    }
  },
  components: {
    SearchLink
  },
  async onLoad () {
    this.categroyData = await this.$request({
      url: '/api/public/v1/categories'
    })
    // console.log(this.categroyData)
  }
}
</script>

<style lang="less">
.category {
  .content {
    position: absolute;
    left: 0;
    right: 0;
    top: 100rpx;
    bottom: 0;
    display: flex;
    margin-top: 20rpx;

    .left {
      width: 198rpx;
      background-color: #fff;
      overflow: scroll;

      &::-webkit-scrollbar {
        /*隐藏滚轮*/
        display: none;
      }

      .cont-item {
        box-sizing: border-box;
        padding: 20rpx 0;
        height: 100rpx;
        line-height: 60rpx;
        border-bottom: 1px solid #eee;
        background-color: #f4f4f4;
        color: #333333;
        text-align: center;

        .cont-text {
          border-left: 8rpx solid transparent;
        }
      }

      .active {
        background-color: #fff;
        color: #eb4450;

        .cont-text {
          height: 60rpx;
          line-height: 60rpx;
          text-align: center;
          border-color: #eb4450;
        }
      }
    }

    .right {
      flex: 1;
      overflow: scroll;
      margin: 0 16rpx;

      &::-webkit-scrollbar {
        /*隐藏滚轮*/
        display: none;
      }

      .title-img {
        width: 520rpx;
        height: 180rpx;
      }

      .inner {
        display: flex;
        // justify-content: center;
        flex-wrap: wrap;

        .title {
          height: 110rpx;
          width: 520rpx;
          line-height: 110rpx;
          text-align: center;

          // background-color: green;
          text {
            margin: 0 30rpx;
            color: #e0e0e0;
          }
        }

        .inner-item {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-bottom: 20rpx;
          font-size: 26rpx;

          image {
            width: 120rpx;
            height: 120rpx;
          }
        }
      }
    }
  }
}
</style>
